
#ifndef _COMMON_H_
#define _COMMON_H_


#include "../engine.h"
#include "../User.h"
//#include "../game/CItem.h"

#include "../NLogic/Item.h"
#include "../NLogic/Bag.h"
#include "../NResource/Static.h"

namespace ProjectZ { namespace NCommand {


class OPEN
{
public:
	static void OnExecute( User * pUser, ::Network::Packet *pPacket )
	{
		PREPARE_PACKET;
		pUser->Initialize();
	}
};

class CLOSE
{
public:
	static void OnExecute( User * pUser, ::Network::Packet *pPacket )
	{
		PREPARE_PACKET;

//		pUser->SetDelUser();
//		pUser->FlushUserInfo();
//		pUser->FlushCharacterInfo();
//		pUser->UnRegistLocation();
	}
};

class CS_PING
{
public:
	static void OnExecute(User * pUser, ::Network::Packet * pPacket)
	{
		PREPARE_PACKET;
		pUser->UpdateLatency();
		//pUser->latency_info.LastPacketTime = NF::GetTickCount();

//		::Logger::GETLOG()->ToAll(::Logger::DEBUG, "CS_PING GID %d, time=%lu, ip1=%s, ip2=%s",
//				pUser->GetGID(), pUser->latency_info.LastPacketTime, pSession->GetNetworkPeerInfo().ip.c_str(), pSession->GetNetworkInfo().ip.c_str());

		//pUser->FlushUserInfo();
		//pUser->FlushCharacterInfo();
		//pUser->UnRegistLocation();

	}
};

class CS_GET_TEST_ITEM
{
public:
	static void OnExecute(User * pUser, ::Network::Packet * pPacket)
	{
		PREPARE_PACKET;

		uint8_t itemQuality = 0;
		int8_t classType = 0;
		uint8_t sheetType = 0;
		uint16_t itemTid = 0;
		uint8_t count = 0;

		pPacket->ParseMsg("%c%c%c%2d%c", &itemQuality, &classType, &sheetType, &itemTid, &count);

		FILELOG("CS_GET_TEST_ITEM : quality=%d, class=%d, sheet=%d, tid=%d", itemQuality, classType, sheetType, itemTid);


		NLogic::Item::Data clsData;
		clsData.tid = itemTid;
		clsData.sub_type = sheetType;
		clsData.quality = itemQuality;
		clsData.class_type = classType;
		clsData.quantity = 1;
		clsData.bag_type = NResource::Static::InstancePtr()->GetItemResource()->GetItemBagType(clsData);

		NResource::Static::InstancePtr()->GetItemResource()->setItemEffect(clsData);

		NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(clsData.bag_type));
		if (NULL == pBag)
		{
			return;
		}

		for (int i = 0; i < count; i++)
		{
			bool bBagFull = false;
			NLogic::Item * pItem = pBag->AddItemWithData(clsData, bBagFull);
			if (NULL == pItem)
				continue;
		}

		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->MsgCat("%2d", ACK_OK);
		pAck->MakePacket(CMD_SC_GET_TEST_ITEM);
		pUser->Send(pAck);
	}
};

class CS_COPY_UPGRADE_STONE
{
public:
	static void OnExecute(User * pUser, ::Network::Packet * pPacket)
	{
		PREPARE_PACKET;

		uint64_t itemSeq = 0;

		*pPacket >> itemSeq;

		NLogic::Bag * pBag = pUser->GetBag(BAG_TYPE_ACCESSORY);
		if (NULL == pBag)
			return;

		NLogic::Item * pItem = pBag->GetItem(itemSeq);
		if (NULL == pItem)
			return;

		NLogic::Item::Data clsData = pItem->GetData();

		bool bBagFull = false;
		pBag->AddItemWithData(clsData, bBagFull, true);
	}
};


class CS_TEST
{
public:
	static void OnExecute(User * pUser, ::Network::Packet * pPacket)
	{
		PREPARE_PACKET;
	}
};

} /* NCommand */ } /* ProjectZ */


#endif /* _COMMON_H_ */
